package cn.sdx.ssm.service;

import cn.sdx.ssm.dao.EmployeeMapper;
import cn.sdx.ssm.pojo.Employee;
import cn.sdx.ssm.pojo.EmployeeExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author sdx
 * @create 2021-05-09 2:26
 */
@Service
public class EmployeeService {
    @Autowired
    private EmployeeMapper mapper;
    public List<Employee> getAllEmp() {
        EmployeeExample example = new EmployeeExample();
        example.setOrderByClause("e.id asc");
        return mapper.selectByExampleWithDept(example);
    }
    /**
     *校验表单数据，判断用户信息是否可用
     * true已存在，不可用，反之可用
     */
    public boolean isExistEmp(Employee emp) {
        EmployeeExample empExample = new EmployeeExample();
        EmployeeExample.Criteria criteria = empExample.createCriteria();
        if (emp.getLastName()!=null){
            criteria.andLastNameEqualTo(emp.getLastName());
        }else if (emp.getEmail()!=null){
            criteria.andEmailEqualTo(emp.getEmail());
        }
        long l = mapper.countByExample(empExample);
        if (l==0){
            return false;
        }
        return true;
    }

    public int insertOneEmp(Employee emp) {
        int count = mapper.insertSelective(emp);
        return count;
    }

    public Employee getEmpById(Integer id) {
        return mapper.selectByPrimaryKey(id);
    }

    public void updateEmp(Employee emp) {
        mapper.updateByPrimaryKeySelective(emp);
    }

    public void deleteEmpById(Integer id) {
        mapper.deleteByPrimaryKey(id);
    }

    public void deleteAllEmp(List<Integer> ids) {
        EmployeeExample example = new EmployeeExample();
        EmployeeExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(ids);
        mapper.deleteByExample(example);
    }
}
